
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

local function IsValid(entity)
	return entity and entity:IsValid()
end

local function PrintError(code)
	print("********************")
	print("* ERROR")
	print("* Code: " .. code)
	print("* Please tell Gareth")
	return false
end

function ENT:Initialize(model)

	self.NumChildLinks = 0
	self.NumChildConnections = 0

	self.IndexData = {}
	self.IndexData.PlayerIndex = 0
	self.IndexData.TrackIndex = 0
	self.IndexData.NodeIndex = 0

	//These are accessed by both client and server and
	//should be initalized in this way
	self:SetNWInt( VAR_HIGHLIGHTING, 0)

	if model then
		self:SetModel(model)
	else
		self:SetModel(Model("models/Combine_Helicopter/helicopter_bomb01.mdl"))
	end

	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:DrawShadow(false)
	self.Entity:SetCollisionGroup(COLLISION_GROUP_WORLD)

	local phys = self.Entity:GetPhysicsObject()
	if phys:IsValid() then
		phys:Sleep()
	end

	if not self.CatmullRomController then
		return self:InitController()
	end

	self:UpdateClient()

end

function ENT:SetIndexData(indexData)

	//Storage on the server probably unnessisary
	self.IndexData = {}
	self.IndexData.PlayerIndex = indexData.playerIdx
	self.IndexData.TrackIndex = indexData.trackIdx
	self.IndexData.NodeIndex = indexData.nodeIdx

	//Update our client so that it can display this data
	self:UpdateClient()
end

//Move down inheritance..
function ENT:UpdateClient()

	self:SetNWInt( VAR_NUM_CHILD_LINKS, self.NumChildLinks)
	self:SetNWInt( VAR_NUM_CHILD_CONNECTIONS, self.NumChildConnections)

	self:SetNWInt( VAR_NODE_INDEX, self.IndexData.NodeIndex)
	self:SetNWString( VAR_TRACK_INDEX, self.IndexData.TrackIndex)
	self:SetNWString( VAR_PLAYER_INDEX, self.IndexData.PlayerIndex)

	self:SetNWVector(VAR_POSITION_LINK_ENDPNT, Vector(0,0,0))
	self:SetNWBool(VAR_DODRAW_LINK_ENDPNT, false)

end

function ENT:Think()

	//Check to see if any link was added by the client in the last frame and handle it
	local newLinkEnt = self:GetNWEntity(VAR_NEW_CHILD_LINK_ENT)
	if (newLinkEnt && newLinkEnt:IsValid()) then

		self.LinkTable[ newLinkEnt ] =	 newLinkEnt
	end

	//Check to see if any connection was added by the client in the last frame and handle it
	local newConnEnt = self:GetNWEntity(VAR_NEW_CHILD_CONNECTION_ENT)
	if (newConnEnt && newConnEnt:IsValid()) then

		self.ConnectionTable[ newConnEnt ] = newConnEnt
	end


end